package com.example.actearly_revised;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.database.SQLException;

public class SQLogic { // this will be the class to call, feel free to look at
						// SQLConnector as a black box

	SQLConnector connector;

	// General DB Connection Methods
	// tested: works!
	public SQLogic(Context c) {
		connector = new SQLConnector(c);
	}

	// tested: works! 
	public void open() throws SQLException // Opens Database
	{
		connector.open();
	}

	// tested: works! 
	public void close() throws SQLException // closes Database 
	{ 
		connector.close();
	}
	
	// Methods Relating to User Table:

	// Creates and/or Updates user profile, built only for one child now but can
	// be changed if needed
	// tested: works! 
	public long UpdateUserEntry(String child, String parent, String birthdate, String doctor, String email, int gender, String docEmail, int childId) throws SQLException
	{
		return connector.UpdateUserEntry(child, parent, birthdate, doctor,email,gender,docEmail,childId);
	}
	
	public long CreateUserEntry(String child, String parent, String birthdate, String doctor, String email, int gender, String docEmail) throws SQLException{
		return connector.CreateUserEntry(child, parent, birthdate, doctor, email, gender, docEmail);
	}
	
	public long DeletUserEntry(int childId) throws SQLException {
		return connector.DeletUserEntry(childId);
	}
	
	public User getUserData(int key) throws SQLException {
		return connector.getUserData(key);
	}
	
	// gets all user profiles on file
	public User[] getAllUserData() throws SQLException {
		return connector.getAllUserData();
	}
	
	// Update the OptIn field in the User table for a user
	// use one for yes, two for no
	//tested: works! 
	public long UpdateOptIn(int key, int opt_value) throws SQLException // assuming here that user is already created
	{
		return connector.UpdateOptIn(key, opt_value);
	}
	
	// method to tell you if the user has opted in to notifications/calander or not
	//tested: works! 
	public boolean HasOptedIn(int key) throws SQLException  // use to check if the user opted-in to updates
	{
		return connector.HasOptedIn(key);
	}
	
	// tells us whether user has opted in or not
	public boolean OptInAnswered(int key) throws SQLException
	{
		return connector.OptInAnswered(key);
	}
	
	// update the date of the last time the user updated thier answers
	public long UpdateLastUpdated(int key, Date update_date) throws SQLException // assuming here that user is already created
	{
		return connector.UpdateLastUpdated(key, update_date);
	}

	// returns the date of the last time the user updated their answers
	// returns null if nothing is there 
	public Date LastUpdated(int key) throws SQLException, ParseException{  
		return connector.LastUpdated(key);
	}

	// update the Last Notified field in User Table
	public long UpdateLastNotified(int key, Date date_notified) throws SQLException // assuming here that user is already created
	{
		return connector.UpdateLastNotified(key, date_notified);
	}
	
	// return the date the user was last notified of something 
	// returns null if no date is recorded
	public Date LastNotified(int key) throws SQLException, ParseException
	{ 
		return connector.LastNotified(key);
	}
	

	// Methods for Question Table,  Questions were loaded into Table in SQLConnector Class
	
	// Returns QuestionsObject, defined as a hashmap, filled with all the questions
	// refer to QuestionsObject.java for available methods, should be easy to use
	// specify the milestone group and urgency type you want (0=not urgent, 1=urgent, 2= urgent but phrased affirmatively)
	public QuestionsObject RetrieveQuestions(int MilestoneAge, int Urgency, String Category) throws SQLException
	{
		return connector.RetrieveQuestions(MilestoneAge, Urgency, Category);
	}
	
	public QuestionsObject RetrieveAllQuestions(int MilestoneAge, int Urgency) throws SQLException
	{
		return connector.RetrieveAllQuestions(MilestoneAge,Urgency);
	}
	
	public void AddQuestions(int Milestone, String Question, int Urgency, String Category){
		connector.AddQuestions(Milestone, Question, Urgency, Category);
	}
	
	// Methods for Answers Table
	
	// updates the Answer table with the given answer
	// QuestionID is the question number, 1-10 now but will be 1-40
	// childId is one for now but could change with multiple children
	public void UpdateAnswer(int QuestionID, String AnswerCode, int childId) throws SQLException
	{
		connector.UpdateAnswer(QuestionID, AnswerCode,childId);
	}
	
	// Gives an AnswersObject (similar to Questions Object) with the answer to a specified
	// child and questionID
	public AnswersObject RetrieveAnswers(int childId,int questionID) throws SQLException // read method
	{
		return connector.RetrieveAnswers(childId,questionID);
	}
	
	// gives all answers for one child
	public AnswersObject RetrieveAnswers(int childId) throws SQLException // read method
	{
		return connector.RetrieveAnswers(childId);
	}
	
	
	public ArrayList<QuestionAnswerGroup> GetQuestionAnswerGroup(int childId, int CurrentMilestone) throws SQLException {
		return connector.GetQuestionAnswerGroup(childId, CurrentMilestone);
	}
	
	// Methods for Journal Table
	
	// Method to Update the Journal Table with a new Entry, Table is designed in a way where it will have
	// a separate journal for for each milestone range
	public void UpdateJournal(int Milestone_in_Monthes, String JournalEntry, int profile) throws SQLException
	{
		connector.UpdateJournal(Milestone_in_Monthes, JournalEntry, profile);
	}
	
	// Pulls Journal Table out into a journal object, just like with Questions and Answers retrive methods  
	public JournalObject RetrieveJournalEntry(int profile) throws SQLException // read method
	{
		return connector.RetrieveJournalEntry(profile);
	}
	
	// query by milestone, all goes into hashmap with hintId as key
	public HintObject RetrieveHelpfulHints(int Milestone_in_Monthes) throws SQLException {
		return connector.RetrieveHelpfulHints(Milestone_in_Monthes);
	}
	
	// Right now simply assuming that this is strictly inserting not updating
	public void AddHelpfulHint(String Hint, int Milestone_in_Monthes) throws SQLException {
		connector.AddHelpfulHint(Hint, Milestone_in_Monthes);
	}
	
	public void RemoveHint(int hintId) throws SQLException {
		connector.RemoveHint(hintId);
	}
	
	public void RemoveAllHints() throws SQLException {
		connector.RemoveAllHints();
	}
	
	public Boolean ifDBExist() throws SQLException {
		return connector.checkDataBase();
		
	}
	public String[] returnQuestions(){
		return  connector.Fill_Questions();
	}
	
}
